


var express = require('express');
const db=require('../db.js')
var router = express.Router();

/* GET home page. */
router.get('/detail/:id', function(req, res, next) {
var id=req.params.id;
var sqlStr ="selsct id,name,price,image,content FROM product where id=?";
var sqlParam=[id];

  //var sql ='SELECT id,name,price,image,add_time FROM product order by add_time desc limit 0,4';
  db.conn.query(sqlStr,sqlParam,function(err,result){
    if(err){console.log('[SELECT ERROR]-',err.message);
      return;
    }
    console.log(result);

    res.render('detail.ejs', { oneProduct:result });
  })
 
});
router.get('/product', function(req, res, next) {
  // 从地址栏获取 cat_id 参数
  var cat_id = req.query.cat_id;  

  if (cat_id) { 
      // 有 cat_id 参数：按分类查询
      var sqlStr = "select * from product where cat_id=? order by id desc";
      var sqlParam = [cat_id];
  } else { 
      // 无 cat_id 参数：查询所有商品
      var sqlStr = "select * from product order by id desc";
      var sqlParam = [];
  }

router.get('/page/:pid',function(req,res,next){
  var pid=req.params.pid;
  var sqlstr="select id,page_name,content from page where id=?";
  var sqlParam=[pid];
  db.conn.query(sqlStr,sqlParam,function(err,result){
    if(err){
      console.log(err);
      return
    }
    console.log(result);
    res.render('page.ejs',{pageContent:result});
  });



})
















  // 执行 SQL 查询
  db.conn.query(sqlStr, sqlParam, function(err, result) {
      if (err) {
          console.log(err);
          return;
      }
      console.log(result); // 打印查询结果（数组形式）
      
      // 渲染模板，传递查询数据给 productList.ejs
      res.render('productList.ejs', { products: result });
  });
});













module.exports = router;
